//
//  AppDelegate.m
//  Liberty Voice
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ClearLove. All rights reserved.
//

#import "AppDelegate.h"
#import "JHAudioViewController.h"
#import "JHCharacterViewController.h"
#import "JHPlayViewController.h"
#import "JHMyColletionViewController.h"
#import "JHPersonalViewController.h"
#import "CustomTabBarController.h"
#import <ShareSDK/ShareSDK.h>
#import "WeiboSDK.h"
#import "NetworkHandle.h"
#import "JHAudio.h"


@interface AppDelegate ()<UITabBarControllerDelegate>


@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//    NSLog(@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"321"]);
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [NetworkHandle startNetMonitoringWithCompletion:^(id result) {
        UIAlertView *msg = [[UIAlertView alloc]initWithTitle:@"提示" message:[result objectForKey:@"info"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [msg show];
        [msg release];
    }];

    
    
    
    JHAudioViewController *j = [[[JHAudioViewController alloc] init] autorelease];
    self.window.rootViewController = j;

    /**
     *  1.创建视图控制器
     */
    JHAudioViewController *JHAudioC = [[JHAudioViewController alloc] init];
    JHCharacterViewController *JHCharacterC = [[JHCharacterViewController alloc] init];

    
//    JHMIViewController *JHMiC = [[JHMIViewController alloc] init];

//    JHPlayViewController *JHPlayC = [[JHPlayViewController alloc] init];
    JHMyColletionViewController *JHMiC = [[JHMyColletionViewController alloc] init];

    JHPersonalViewController *JHpersonalC = [[JHPersonalViewController alloc] init];
    JHPersonalViewController *JHPicC = [[JHPersonalViewController alloc] init];
                                             
    /**
     *  2.创建导航栏
     */
    UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:JHAudioC];
    UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:JHCharacterC];

    UINavigationController *navi4 = [[UINavigationController alloc] initWithRootViewController:JHMiC];
    UINavigationController *navi5 = [[UINavigationController alloc] initWithRootViewController:JHpersonalC];

 
    navi1.navigationBar.translucent = NO;
    navi2.navigationBar.translucent = NO;

    navi4.navigationBar.translucent = NO;
    navi5.navigationBar.translucent = NO;

    /**
     *  3. 创建标签
     */
    // 音频页
    JHAudioC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"音乐" image:[UIImage imageNamed:@"iconfont-yinle-2"] tag:1000];
    // 阅读页
    JHCharacterC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"幽默" image:[UIImage imageNamed:@"iconfont-zhayan"] tag:1001];
//    // 播放页
//    JHPlayC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"播放" image:[UIImage imageNamed:@"iconfont-tuiguangzhong"] tag:1002];
    // 搜索页
    JHMiC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"分类" image:[UIImage imageNamed:@"iconfont-moban"] tag:1003];
    // 个人页
    JHpersonalC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"iconfont-xiaozhushou"] tag:1004];
    
    // 创建标签控制器
    CustomTabBarController *tabBar = [[CustomTabBarController alloc] init];
    // 设置tabBar不透明
    tabBar.tabBar.translucent = NO;
    // 设置代理人
    tabBar.delegate = self;
    // 设置需要被标签控制器管理的控制器
//    tabBar.viewControllers = [NSArray arrayWithObjects:
//                            [tabBar viewControllerWithTabTitle:@"" image:nil className:@"UIViewController"],nil];
    tabBar.viewControllers = @[navi1, navi2,[tabBar viewControllerWithTabTitle:@"" image:nil className:@"UIViewController"], navi4, navi5];
    [tabBar addCenterBackButtonWithImage:[UIImage imageNamed:@"7A200BDF-2E33-4925-AC63-DD24F3185993"] highlightImage:[UIImage imageNamed:@"7A200BDF-2E33-4925-AC63-DD24F3185993"]];
    // 把标签控制器设置为根视图控制器
    self.window.rootViewController = tabBar;
    
    // 释放
    [JHAudioC release];
    [JHCharacterC release];
    
    [JHMiC release];
    [JHpersonalC release];
    [JHPicC release];
    [navi1 release];
    [navi2 release];
    
    [navi4 release];
    [navi5 release];
    [tabBar release];

    [_window release];
    
    //1.初始化ShareSDK应用,字符串"4a88b2fb067c"换成你申请的ShareSDK应用的Appkey，这个key来源于ShareSDK官网上申请获得
    [ShareSDK registerApp:@"7bb6480e8447"];
    
    //2. 初始化社交平台
    [self initializePlat];

    
    
    return YES;
}


- (void)initializePlat
{
    //初始化新浪，在新浪微博开放平台上申请应用
    [ShareSDK connectSinaWeiboWithAppKey:@"3932319228" appSecret:@"b2aa83f6d8a48ea42602d1af72cb9bf5" redirectUri:@"http://www.baidu.com" weiboSDKCls:[WeiboSDK class]];
    
}

//添加两个回调方法,return的必须要ShareSDK的方法
- (BOOL)application:(UIApplication *)application
      handleOpenURL:(NSURL *)url
{
    return [ShareSDK handleOpenURL:url
                        wxDelegate:self];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return [ShareSDK handleOpenURL:url
                 sourceApplication:sourceApplication
                        annotation:annotation
                        wxDelegate:self];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    NSLog(@"I'm About to leave...");
//    JHPlayViewController *p = [JHPlayViewController sharePlayerController];
  
//    NSMutableSet *set = [[NSMutableSet alloc] init];
//    for (JHAudio * a in p.arr) {
//        [set addObject:a];
//    }
//    [p.arr removeAllObjects];
//    for (NSString *str in set) {
//        [p.arr addObject:str];
//    }
    
    
//    [[NSUserDefaults standardUserDefaults]setObject:@"123" forKey:@"321"];
}

@end
